home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / mac_gdiff 1.51 / gdiff-mac1.src.1.15 / diff.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-18  |  10.6 KB  |  395 lines  |  [TEXT/????]

  1. /* Shared definitions for GNU DIFF
  2.    Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU DIFF is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU DIFF; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <ctype.h>
  22. #include <stdio.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25.  
  26. #ifdef USG
  27. #include <time.h>
  28. #ifdef HAVE_NDIR
  29. #ifdef NDIR_IN_SYS
  30. #include <sys/ndir.h>
  31. #else
  32. #include <ndir.h>
  33. #endif /* not NDIR_IN_SYS */
  34. #else
  35. #include <dirent.h>
  36. #endif /* not HAVE_NDIR */
  37. #include <fcntl.h>
  38. #ifndef HAVE_DIRECT
  39. #define direct dirent
  40. #endif
  41. #else /* not USG */
  42. #include <sys/time.h>
  43. #include <sys/dir.h>
  44. #include <sys/file.h>
  45. #endif
  46.  
  47. #ifdef USG
  48. /* Define needed BSD functions in terms of sysV library.  */
  49.  
  50. #define BTHINGS
  51. #ifndef XENIX
  52. #define dup2(f,t)    (close(t),fcntl((f),F_DUPFD,(t)))
  53. #endif
  54.  
  55. #define vfork    fork
  56. #define index    strchr
  57. #define rindex    strrchr
  58. #endif
  59.  
  60. #ifdef MAC
  61. #define BTHINGS
  62. #include <stdlib.h>
  63. #define main gdiff_main
  64. #define open xx_open
  65. #include "unix.h"
  66. #undef open
  67. #define DIRECTORY_SEPERATOR_STRING ":"
  68. #define DIRECTORY_SEPERATOR_CHAR ':'
  69. #endif
  70.  
  71. #ifndef DIRECTORY_SEPERATOR_STRING
  72. #define DIRECTORY_SEPERATOR_STRING "/"
  73. #define DIRECTORY_SEPERATOR_CHAR '/'
  74. #endif
  75.  
  76. #ifdef BTHINGS
  77. #include <string.h>
  78. #define bcopy(s,d,n)    memcpy((d),(s),(n))
  79. #define bcmp(s1,s2,n)    memcmp((s1),(s2),(n))
  80. #define bzero(s,n)    memset((s),0,(n))
  81. #endif
  82.  
  83. #ifdef sparc
  84. /* vfork clobbers registers on the Sparc, so don't use it.  */
  85. #define vfork fork
  86. #endif
  87.  
  88. #include <errno.h>
  89. extern int      errno;
  90. extern int      sys_nerr;
  91. extern char    *sys_errlist[];
  92.  
  93. #define    EOS        (0)
  94. #ifndef FALSE
  95. #define    FALSE        (0)
  96. #endif
  97. #ifndef TRUE
  98. #define TRUE        1
  99. #endif
  100.  
  101. #define min(a,b) ((a) <= (b) ? (a) : (b))
  102. #define max(a,b) ((a) >= (b) ? (a) : (b))
  103.  
  104. #ifndef PR_FILE_NAME
  105. #define PR_FILE_NAME "/bin/pr"
  106. #endif
  107.  
  108. /* Support old-fashioned C compilers.  */
  109. #if defined (__STDC__) || defined (__GNUC__)
  110. #include "limits.h"
  111. #else
  112. #define INT_MAX 2147483647
  113. #define CHAR_BIT 8
  114. #endif
  115.  
  116. /* Support old-fashioned C compilers.  */
  117. #if !defined (__STDC__) && !defined (__GNUC__)
  118. #define const
  119. #endif
  120.  
  121. #ifndef O_RDONLY
  122. #define O_RDONLY 0
  123. #endif
  124.  
  125. /* Variables for command line options */
  126.  
  127. #ifndef GDIFF_MAIN
  128. #define EXTERN extern
  129. #else
  130. #define EXTERN
  131. #endif
  132.  
  133. enum output_style {
  134.   /* Default output style.  */
  135.   OUTPUT_NORMAL,
  136.   /* Output the differences with lines of context before and after (-c).  */
  137.   OUTPUT_CONTEXT,
  138.   /* Output the differences in a unified context diff format (-u). */
  139.   OUTPUT_UNIFIED,
  140.   /* Output the differences as commands suitable for `ed' (-e).  */
  141.   OUTPUT_ED,
  142.   /* Output the diff as a forward ed script (-f).  */
  143.   OUTPUT_FORWARD_ED,
  144.   /* Like -f, but output a count of changed lines in each "command" (-n). */
  145.   OUTPUT_RCS,
  146.   /* Output merged #ifdef'd file (-D).  */
  147.   OUTPUT_IFDEF };
  148.  
  149. /* True for output styles that are robust,
  150.    i.e. can handle a file that ends in a non-newline.  */
  151. #define ROBUST_OUTPUT_STYLE(S) \
  152.  ((S) == OUTPUT_CONTEXT || (S) == OUTPUT_UNIFIED || (S) == OUTPUT_RCS \
  153.   || (S) == OUTPUT_NORMAL)
  154.  
  155. EXTERN enum output_style output_style;
  156.  
  157. /* Number of lines of context to show in each set of diffs.
  158.    This is zero when context is not to be shown.  */
  159. EXTERN int      context;
  160.  
  161. /* Consider all files as text files (-a).
  162.    Don't interpret codes over 0177 as implying a "binary file".  */
  163. EXTERN int    always_text_flag;
  164.  
  165. /* Ignore changes in horizontal whitespace (-b).  */
  166. EXTERN int      ignore_space_change_flag;
  167.  
  168. /* Ignore all horizontal whitespace (-w).  */
  169. EXTERN int      ignore_all_space_flag;
  170.  
  171. /* Ignore changes that affect only blank lines (-B).  */
  172. EXTERN int      ignore_blank_lines_flag;
  173.  
  174. /* Ignore changes that affect only lines matching this regexp (-I).  */
  175. EXTERN char    *ignore_regexp;
  176.  
  177. /* Result of regex-compilation of `ignore_regexp'.  */
  178. EXTERN struct re_pattern_buffer ignore_regexp_compiled;
  179.  
  180. /* 1 if lines may match even if their lengths are different.
  181.    This depends on various options.  */
  182. EXTERN int      length_varies;
  183.  
  184. /* Ignore differences in case of letters (-i).  */
  185. EXTERN int      ignore_case_flag;
  186.  
  187. /* File labels for `-c' output headers (-L).  */
  188. EXTERN char *file_label[2];
  189.  
  190. /* Regexp to identify function-header lines (-F).  */
  191. EXTERN char    *function_regexp;
  192.  
  193. /* Result of regex-compilation of `function_regexp'.  */
  194. EXTERN struct re_pattern_buffer function_regexp_compiled;
  195.  
  196. /* Say only whether files differ, not how (-q).  */
  197. EXTERN int     no_details_flag;
  198.  
  199. /* Report files compared that match (-s).
  200.    Normally nothing is output when that happens.  */
  201. EXTERN int      print_file_same_flag;
  202.  
  203. /* character that ends a line.  Currently this is always `\n'.  */
  204. EXTERN char     line_end_char;
  205.  
  206. /* Output the differences with exactly 8 columns added to each line
  207.    so that any tabs in the text line up properly (-T).  */
  208. EXTERN int    tab_align_flag;
  209.  
  210. /* Expand tabs in the output so the text lines up properly
  211.    despite the characters added to the front of each line (-t).  */
  212. EXTERN int    tab_expand_flag;
  213.  
  214. /* In directory comparison, specify file to start with (-S).
  215.    All file names less than this name are ignored.  */
  216. EXTERN char    *dir_start_file;
  217.  
  218. /* If a file is new (appears in only one dir)
  219.    include its entire contents (-N).
  220.    Then `patch' would create the file with appropriate contents.  */
  221. EXTERN int    entire_new_file_flag;
  222.  
  223. /* Pipe each file's output through pr (-l).  */
  224. EXTERN int    paginate_flag;
  225.  
  226. /* String to use for #ifdef (-D).  */
  227. EXTERN char *    ifdef_string;
  228.  
  229. /* String containing all the command options diff received,
  230.    with spaces between and at the beginning but none at the end.
  231.    If there were no options given, this string is empty.  */
  232. EXTERN char *    switch_string;
  233.  
  234. /* Nonzero means use heuristics for better speed.  */
  235. EXTERN int    heuristic;
  236.  
  237. /* Name of program the user invoked (for error messages).  */
  238. EXTERN char *    program;
  239.  
  240. /* The result of comparison is an "edit script": a chain of `struct change'.
  241.    Each `struct change' represents one place where some lines are deleted
  242.    and some are inserted.
  243.    
  244.    LINE0 and LINE1 are the first affected lines in the two files (origin 0).
  245.    DELETED is the number of lines deleted here from file 0.
  246.    INSERTED is the number of lines inserted here in file 1.
  247.  
  248.    If DELETED is 0 then LINE0 is the number of the line before
  249.    which the insertion was done; vice versa for INSERTED and LINE1.  */
  250.  
  251. struct change
  252. {
  253.   struct change *link;        /* Previous or next edit command  */
  254.   int inserted;            /* # lines of file 1 changed here.  */
  255.   int deleted;            /* # lines of file 0 changed here.  */
  256.   int line0;            /* Line number of 1st deleted line.  */
  257.   int line1;            /* Line number of 1st inserted line.  */
  258.   char ignore;            /* Flag used in context.c */
  259. };
  260.  
  261. /* Structures that describe the input files.  */
  262.  
  263. /* Data on one line of text.  */
  264.  
  265. struct line_def {
  266.     char        *text;
  267.     int         length;
  268.     unsigned    hash;
  269. };
  270.  
  271. /* Data on one input file being compared.  */
  272.  
  273. struct file_data {
  274.     int             desc;    /* File descriptor  */
  275.     char           *name;    /* File name  */
  276.     struct stat     stat;    /* File status from fstat()  */
  277.     int             dir_p;    /* 1 if file is a directory  */
  278.  
  279.     /* Buffer in which text of file is read.  */
  280.     char *        buffer;
  281.     /* Allocated size of buffer.  */
  282.     int            bufsize;
  283.     /* Number of valid characters now in the buffer. */
  284.     int            buffered_chars;
  285.  
  286.     /* Array of data on analyzed lines of this chunk of this file.  */
  287.     struct line_def *linbuf;
  288.  
  289.     /* Allocated size of linbuf array (# of elements).  */
  290.     int            linbufsize;
  291.  
  292.     /* Number of elements of linbuf containing valid data. */
  293.     int            buffered_lines;
  294.  
  295.     /* Pointer to end of prefix of this file to ignore when hashing. */
  296.     char *prefix_end;
  297.  
  298.     /* Count of lines in the prefix. */
  299.     int prefix_lines;
  300.  
  301.     /* Pointer to start of suffix of this file to ignore when hashing. */
  302.     char *suffix_begin;
  303.  
  304.     /* Count of lines in the suffix. */
  305.     int suffix_lines;
  306.  
  307.     /* Vector, indexed by line number, containing an equivalence code for
  308.        each line.  It is this vector that is actually compared with that
  309.        of another file to generate differences. */
  310.     int           *equivs;
  311.  
  312.     /* Vector, like the previous one except that
  313.        the elements for discarded lines have been squeezed out.  */
  314.     int           *undiscarded;
  315.  
  316.     /* Vector mapping virtual line numbers (not counting discarded lines)
  317.        to real ones (counting those lines).  Both are origin-0.  */
  318.     int           *realindexes;
  319.  
  320.     /* Total number of nondiscarded lines. */
  321.     int            nondiscarded_lines;
  322.  
  323.     /* Vector, indexed by real origin-0 line number,
  324.        containing 1 for a line that is an insertion or a deletion.
  325.        The results of comparison are stored here.  */
  326.     char       *changed_flag;
  327.  
  328.     /* 1 if file ends in a line with no final newline. */
  329.     int            missing_newline;
  330.  
  331.     /* 1 more than the maximum equivalence value used for this or its
  332.        sibling file. */
  333.     int equiv_max;
  334.  
  335.     /* Table translating diff's internal line numbers 
  336.        to actual line numbers in the file.
  337.        This is needed only when some lines have been discarded.
  338.        The allocated size is always linbufsize
  339.        and the number of valid elements is buffered_lines.  */
  340.     int           *ltran;
  341. };
  342.  
  343. /* Describe the two files currently being compared.  */
  344.  
  345. EXTERN struct file_data files[2];
  346.  
  347. /* Queue up one-line messages to be printed at the end,
  348.    when -l is specified.  Each message is recorded with a `struct msg'.  */
  349.  
  350. struct msg
  351. {
  352.   struct msg *next;
  353.   char *format;
  354.   char *arg1;
  355.   char *arg2;
  356. };
  357.  
  358. /* Head of the chain of queues messages.  */
  359.  
  360. EXTERN struct msg *msg_chain;
  361.  
  362. /* Tail of the chain of queues messages.  */
  363.  
  364. EXTERN struct msg *msg_chain_end;
  365.  
  366. /* Stdio stream to output diffs to.  */
  367.  
  368. EXTERN FILE *outfile;
  369.  
  370. /* Declare various functions.  */
  371.  
  372. #ifdef __STDC__
  373. #define VOID void
  374. #else
  375. #define VOID char
  376. #endif
  377. VOID *xmalloc ();
  378. VOID *xrealloc ();
  379. char *concat ();
  380. void free ();
  381. char *rindex ();
  382. char *index ();
  383.  
  384. void analyze_hunk ();
  385. void error ();
  386. void fatal ();
  387. void message ();
  388. void perror_with_name ();
  389. void pfatal_with_name ();
  390. void print_1_line ();
  391. void print_message_queue ();
  392. void print_number_range ();
  393. void print_script ();
  394. void translate_range ();
  395.